package com.zmt.content.api;

import com.zmt.content.dto.AddCourseDto;
import com.zmt.content.dto.CourseBaseInfoDto;
import com.zmt.content.dto.EditCourseDto;
import com.zmt.content.po.CourseBase;
import com.zmt.content.utils.SecurityUtil;
import com.zmt.xuechengplusbase.exception.ValidationGroups;
import com.zmt.xuechengplusbase.model.PageParams;
import com.zmt.xuechengplusbase.model.PageResult;
import com.zmt.content.dto.QueryCourseParamsDto;
import com.zmt.content.service.CourseBaseService;
import com.zmt.xuechengplusbase.utils.StringUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.bind.BindResult;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;


@Api(value = "课程信息编辑接口",tags = "课程信息编辑接口")
@RestController
public class CourseBaseInfoController {

    @Autowired
    private CourseBaseService courseBaseService;

    @ApiOperation("课程分页查询")
    @PostMapping("/course/list")
    @PreAuthorize("hasAuthority('xc_teachmanager_course_list')")
    public PageResult list(PageParams pageParams,@RequestBody(required = false) QueryCourseParamsDto courseParamsDto){
        SecurityUtil.XcUser user = SecurityUtil.getUser();
        Long company=null;
        String companyId = user.getCompanyId();
        if (StringUtil.isNotEmpty(companyId)){
            company = Long.parseLong(companyId);
        }
        PageResult<PageResult> result = courseBaseService.queryCourseBaseList(company,pageParams, courseParamsDto);
        return result;
    }

    @ApiOperation("新增课程")
    @PostMapping("/course")
    public CourseBaseInfoDto createCourseBase(@RequestBody @Validated/*(ValidationGroups.Insert.class) */AddCourseDto addCourseDto){

        //所属机构的id单点登录后才会获得，目前硬编码一下
        Long companyId=1232141425L;

        CourseBaseInfoDto courseBase = courseBaseService.createCourseBase(companyId, addCourseDto);
        return courseBase;
    }

    @ApiOperation("查询课程信息")
    @GetMapping("/course/{id}")
    public CourseBaseInfoDto queryCourseById(@PathVariable Long id){
//        Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        SecurityUtil.XcUser user = SecurityUtil.getUser();
        String name = user.getName();
        return courseBaseService.getCourseBaseInfo(id);

    }

    @ApiOperation("修改课程信息")
    @PutMapping("/course")
    public CourseBaseInfoDto updateCourse(@RequestBody @Validated/*(ValidationGroups.Update.class)*/ EditCourseDto editCourseDto){
        Long companyId=1232141425L;
        return courseBaseService.updateCourse(companyId,editCourseDto);
    }


    @ApiOperation("删除课程结口")
    @DeleteMapping("/course/{courseId}")
    public void deleteCourse(@PathVariable Long courseId){
        courseBaseService.deleteCourse(courseId);

    }
}
